//
//  YueXiViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "YueXiViewController.h"

@interface YueXiViewController ()<UIAlertViewDelegate>
{
    NSInteger _selectBtn;
    UIDatePicker *datePicker ;
}
@end

@implementation YueXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"包月洗";
    _selectBtn=100;
    self.buyYueXi.selected=YES;
    self.buyYueXi.selected=NO;
    //开始时间
    NSString *startDateString=[self getDateStringFromDate:[NSDate date]];
    [_start setTitle:startDateString forState:UIControlStateNormal];
    //结束时间
    NSDate *dateFuture=[self getPriousorLaterDateFromDate:[NSDate date] withMonth:1];
    NSString *endDateString=[self getDateStringFromDate:dateFuture];
    [_end setTitle:endDateString forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (IBAction)sureOrder:(id)sender {
}
//选择开始时间
- (IBAction)startTime:(id)sender {
    [self setUpDatePicker];
}

- (IBAction)aboutJingJing:(id)sender {
    WebViewController *web=[[WebViewController alloc]init];
    web.pageUrl=ABOUT_JingJing;
    web.title=@"净净包月洗";
    [self.navigationController pushViewController:web animated:YES];
}
//购买包月洗
- (IBAction)clickOnWashBtn:(id)sender {
    UIButton *btn=sender;
    if (_selectBtn==btn.tag) {
        return;
    }
    if (btn.selected==NO) {
        btn.selected=YES;
    }else
    {
       btn.selected=YES;
    }
    _selectBtn=btn.tag;
    
}
//我的包月洗
- (IBAction)clickOnMyWashing:(id)sender {
    UIButton *btn=sender;
    if (_selectBtn==btn.tag) {
        return;
    }
    if (btn.selected==NO) {
        btn.selected=YES;
    }else
    {
        btn.selected=YES;
    }
    _selectBtn=btn.tag;
}
//选择结束时间
- (IBAction)endTime:(id)sender {
    [self setUpDatePicker];
}

- (IBAction)YouHuiQuan:(id)sender {
}
//时间选择
#pragma merk--创建时间选择器
-(void)setUpDatePicker
{
    datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    NSDate *dateMin=[NSDate date];
    //最小时间
    datePicker.minimumDate=dateMin;
    //最大时间是当前天的后三天
    NSDate *dateMax=[NSDate dateWithTimeIntervalSinceNow:2*24*60*60];
    datePicker.maximumDate=dateMax;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        datePicker.frame=CGRectMake(10, alert.bounds.size.height, 320, 216);
        alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20);
        [self.view addSubview:datePicker];
        [alert show];
    }else
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alert.view addSubview:datePicker];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [ZMTool showAlertWithDifferentVersioninWhichViewController:self withTitle:@"温馨提示" andMessage:@"开始时间必须大于当前时间，可选择往后三天以内哦～" andSureTitle:nil andCancleTitle:@"确定" isDelegate:NO andSureBlock:nil andCancleBlock:nil];
            //开始时间
            NSString *startDateString=[self getDateStringFromDate:datePicker.date];
            [_start setTitle:startDateString forState:UIControlStateNormal];
            //结束时间
            NSDate *dateFuture=[self getPriousorLaterDateFromDate:datePicker.date withMonth:1];
            NSString *endDateString=[self getDateStringFromDate:dateFuture];
            [_end setTitle:endDateString forState:UIControlStateNormal];
        }];
        //求出当天的时间字符串
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:ok];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];

        
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        //开始时间
        NSString *startDateString=[self getDateStringFromDate:datePicker.date];
        [_start setTitle:startDateString forState:UIControlStateNormal];
        //结束时间
        NSDate *dateFuture=[self getPriousorLaterDateFromDate:datePicker.date withMonth:1];
        NSString *endDateString=[self getDateStringFromDate:dateFuture];
        [_end setTitle:endDateString forState:UIControlStateNormal];
 
    }
}
#pragma mark--计算一个月之后的时间
-(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month

{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    [comps setMonth:month];
    
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    
    return mDate;
    
}
#pragma mark--转换时间
-(NSString *)getDateStringFromDate:(NSDate *)date
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM月dd日"];//设定时间格式
    NSString *startDateString = [dateFormat stringFromDate:date];
    return startDateString;
}
#pragma mark--进来和出去的一些设置
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    //刷新数据

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
    
}
@end

